package config

import (
	"os"
	"telegram-bot-c9121/internal/logger"

	"github.com/pkg/errors"
	"gopkg.in/yaml.v3"
)

const configFile = "data/config.yaml"

type Config struct {
	BotToken string `yaml:"bot_token"`
}

type Service struct {
	config Config
}

// Return a new service
func New() (*Service, error) {
	s := &Service{}

	rawYaml, err := os.ReadFile(configFile)
	if err != nil {
		logger.Error("Error reading config file", err)
		return nil, errors.Wrap(err, "reading config file")
	}

	err = yaml.Unmarshal(rawYaml, &s.config)
	if err != nil {
		logger.Error("Error parsing config file", err)
		return nil, errors.Wrap(err, "parsing config file")
	}

	return s, nil
}

// Return the bot token
func (s *Service) BotToken() string {
	return s.config.BotToken
}

// Return the config
func (s *Service) GetConfig() Config {
	return s.config
}
